package com.sinodata.bsm.center.purview;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.sino.uam.rbac.AbstractUAMRequest;

import com.sino.uam.rbac.model.AppSystem;
import com.sino.uam.rbac.model.Privilege;
import com.sino.uam.rbac.model.Resource;
import com.sinodata.bsm.center.bean.RelationHelper;
import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.cache.*;
import com.sinodata.bsm.center.service.performance.RealTimeService;
import com.sinodata.bsm.center.service.resource.*;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.center.util.system.SystemContext;
import com.sinodata.bsm.center.web.action.resource.tree.NodePrivilegeUtil;
import com.sinodata.bsm.common.vo.MonitorType;
import com.sinodata.bsm.common.vo.RealTimeViewConf;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResView;
import com.sinodata.bsm.common.vo.ResViewRel;
import com.sinodata.bsm.common.vo.TopuCatalog;
import com.sinodata.bsm.common.vo.TopuConf;

/**
 * 
 * <p>
 * Description:UAM反向请求数据，第一次请求getRootResource 最多2层，再通过getResourceByParent异步取数据。
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-27 15:29:55      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class UAMRequest extends AbstractUAMRequest {

    /*
     * (non-Javadoc)
     * @see com.sino.uam.rbac.AbstractUAMRequest#getAppSystem()
     */
    @Override
    public AppSystem getAppSystem() {
        AppSystem appSystem = new AppSystem(SystemContext.getProperty(FunctionConstants.SYSTEM_ID_KEY).toString(), SystemContext.getProperty(FunctionConstants.SYSTEM_NAME_KEY).toString());
        return appSystem;
    }

    /*
     * (non-Javadoc)
     * @see com.sino.uam.rbac.AbstractUAMRequest#getPrivileges()
     */
    @Override
    public List<Privilege> getPrivileges() {
        List<Privilege> list = new ArrayList<Privilege>();
        list.add(new Privilege(FunctionConstants.TOPO_MANAGER, "拓扑管理", FunctionConstants.TYPE_SYSTEM_FUNCTION));
        list.add(new Privilege(FunctionConstants.VIEW_REPORT, "报表管理", FunctionConstants.TYPE_SYSTEM_FUNCTION));
        list.add(new Privilege(FunctionConstants.STRATEGY_MANAGER, "策略管理", FunctionConstants.TYPE_SYSTEM_FUNCTION));
        list.add(new Privilege(FunctionConstants.SYSTEM_MANAGER, "系统维护", FunctionConstants.TYPE_SYSTEM_FUNCTION));

        list.add(new Privilege(FunctionConstants.VIEW_VIEW, "是否可见", FunctionConstants.TYPE_VIEW_FUNCTION));
        list.add(new Privilege(FunctionConstants.VIEW_RES, "查看资源", FunctionConstants.TYPE_VIEW_FUNCTION));
        list.add(new Privilege(FunctionConstants.EDIT_RES, "资源维护", FunctionConstants.TYPE_VIEW_FUNCTION));
        list.add(new Privilege(FunctionConstants.HANDLE_EVENT, "事件处理", FunctionConstants.TYPE_VIEW_FUNCTION));

        list.add(new Privilege(FunctionConstants.VIEW_RES, "查看资源", FunctionConstants.TYPE_RES_FUNCTION));
        list.add(new Privilege(FunctionConstants.EDIT_RES, "资源维护", FunctionConstants.TYPE_RES_FUNCTION));
        list.add(new Privilege(FunctionConstants.HANDLE_EVENT, "事件处理", FunctionConstants.TYPE_RES_FUNCTION));
        list.add(new Privilege(FunctionConstants.VIEW_TOPO, "是否可见", FunctionConstants.TYPE_TOPO_FUNCTION));
        list.add(new Privilege(FunctionConstants.VIEW_REAL_TIME, "是否可见", FunctionConstants.TYPE_REAL_TIME_FUNCTION));

        return list;
    }

    /*
     * (non-Javadoc)
     * @see com.sino.uam.rbac.AbstractUAMRequest#getResourceByParent(java.lang.String)
     */
    @Override
    public List<Resource> getResourceByParent(String parentId) {
        List<Resource> results = new ArrayList<Resource>();
        if (parentId.indexOf("BSM_C_") != -1) {
            Long id = Long.parseLong(parentId.substring(parentId.lastIndexOf("_") + 1));
            // 说明父资源是拓扑目录
            List<TopuCatalog> topoCatalogList = SpringContextHolder.getBean(TopuCatalogCache.class).findAll();
            if (topoCatalogList != null) {
                for (TopuCatalog topuCatalog : topoCatalogList) {
                    Long catalogId = topuCatalog.getId();
                    if (topuCatalog.getParent() == null || topuCatalog.getParent().longValue() != id) {
                        continue;
                    }
                    Resource resource = new Resource("BSM_C_" + catalogId, topuCatalog.getName(), FunctionConstants.TYPE_TOPO_FUNCTION, "" + catalogId, parentId);
                    boolean hasChild = SpringContextHolder.getBean(TopuCatalogCache.class).hasChildTopuCatalog(catalogId);

                    List<TopuConf> conflist = SpringContextHolder.getBean(TopuConfCache.class).findByCatalogId(catalogId);
                    for (TopuConf conf : conflist) {
                        Resource confResource = new Resource("BSM_T_" + conf.getId(), conf.getName(), FunctionConstants.TYPE_TOPO_FUNCTION, "" + conf.getId(), "BSM_C_" + catalogId);
                        resource.getChildList().add(confResource);
                    }
                    resource.setLeaf((!hasChild && conflist.size() == 0));
                    results.add(resource);
                }
            }
            List<TopuConf> conflist = SpringContextHolder.getBean(TopuConfCache.class).findByCatalogId(id);
            for (TopuConf conf : conflist) {
                Resource confResource = new Resource("BSM_T_" + conf.getId(), conf.getName(), FunctionConstants.TYPE_TOPO_FUNCTION, "" + conf.getId(), parentId);
                results.add(confResource);
            }

        } else if (parentId.indexOf("BSM_R_") != -1) {
            Long id = Long.parseLong(parentId.substring(parentId.lastIndexOf("_") + 1));
            // 说明父资源是 监控资源对象
            List<ResBean> children = RelationHelper.children(id);
            if (children.size() > 0) {
                for (ResBean resBean : children) {
                    Resource res = new Resource("BSM_R_" + resBean.res().getId(), resBean.res().getName(), FunctionConstants.TYPE_RES_FUNCTION, "R_" + resBean.res().getId(), parentId);
                    List<ResBean> clist = RelationHelper.children(resBean.res().getId());
                    res.setLeaf(clist.size() > 0 ? false : true);
                    results.add(res);
                }
            }
        } else if (parentId.indexOf("BSM_X_") != -1) {
            List<RealTimeViewConf> realTimeViewConfList = SpringContextHolder.getBean(RealTimeService.class).findAllFreeCustomView();
            for (RealTimeViewConf realTimeViewConf : realTimeViewConfList) {
                Resource realTimeConfResource = new Resource("BSM_X_" + realTimeViewConf.getId(), realTimeViewConf.customName(), FunctionConstants.TYPE_REAL_TIME_FUNCTION, "X_" + realTimeViewConf.getId(), "BSM_X_REALTIME");
                results.add(realTimeConfResource);
            }
        } else if (parentId.indexOf("BSM_M_") != -1) {
            //资源分类
            //            List<RealTimeViewConf> realTimeViewConfList = SpringContextHolder.getBean(RealTimeService.class).findAllFreeCustomView();
            //            for (RealTimeViewConf realTimeViewConf : realTimeViewConfList) {
            //                Resource realTimeConfResource = new Resource("BSM_X_" + realTimeViewConf.getId(), realTimeViewConf.customName(), FunctionConstants.TYPE_REAL_TIME_FUNCTION, "X_" + realTimeViewConf.getId(), "BSM_X_REALTIME");
            //                results.add(realTimeConfResource);
            //            }
            Collection<Res> ress = SpringContextHolder.getBean(ResService.class).findAll();
            Long id = Long.parseLong(parentId.substring(parentId.lastIndexOf("_") + 1));
            for (Res res : ress) {
                Long mt = SpringContextHolder.getBean(ResTypeCache.class).getMoitTypeId(res.getResTypeId());
                if (mt != null && mt.longValue() == id) {
                    Resource cres = new Resource("BSM_R_" + res.getId(), res.getName(), FunctionConstants.TYPE_RES_FUNCTION, "R_" + res.getId(), parentId);
                    List<ResBean> clist = RelationHelper.children(res.getId());
                    cres.setLeaf(clist.size() > 0 ? false : true);
                    results.add(cres);
                }

            }
        }
        return results;
    }

    /*
     * (non-Javadoc)
     * @see com.sino.uam.rbac.AbstractUAMRequest#getRootResource()
     */
    @Override
    public List<Resource> getRootResource() {
        List<Resource> list = new ArrayList<Resource>();
        // 系统功能权限
        Resource systemResource = new Resource("BSM_SYSTEM", "系统功能权限", FunctionConstants.TYPE_SYSTEM_FUNCTION, FunctionConstants.TYPE_SYSTEM_FUNCTION, null);
        systemResource.setLeaf(true);
        list.add(systemResource);
        // 拓扑及拓扑目录树
        Resource topoResource = new Resource("BSM_TOPO", "拓扑和性能图目录", FunctionConstants.TYPE_TOPO_FUNCTION, FunctionConstants.TYPE_TOPO_FUNCTION, null);
        topoResource.setLeaf(false);
        List<TopuCatalog> topoCatalogList = SpringContextHolder.getBean(TopuCatalogCache.class).findAll();
        if (topoCatalogList != null) {
            for (TopuCatalog topuCatalog : topoCatalogList) {
                if (topuCatalog.getParent() != null) {
                    continue;
                }
                Long catalogId = topuCatalog.getId();
                Resource resource = new Resource("BSM_C_" + catalogId, topuCatalog.getName(), FunctionConstants.TYPE_TOPO_FUNCTION, "" + catalogId, "BSM_TOPO");
                boolean hasChild = SpringContextHolder.getBean(TopuCatalogCache.class).hasChildTopuCatalog(catalogId);
                List<TopuConf> conflist = SpringContextHolder.getBean(TopuConfCache.class).findByCatalogId(catalogId);
                resource.setLeaf((!hasChild && conflist.size() == 0));
                topoResource.getChildList().add(resource);
            }
        }
        //性能图
        List<RealTimeViewConf> realTimeViewConfList = SpringContextHolder.getBean(RealTimeService.class).findAllFreeCustomView();
        if (realTimeViewConfList != null && realTimeViewConfList.size() > 0) {
            Resource realTimeResource = new Resource("BSM_X_REALTIME", "性能图目录", FunctionConstants.TYPE_REAL_TIME_FUNCTION, "BSM_X_REALTIME", "BSM_TOPO");
            realTimeResource.setLeaf(realTimeViewConfList.size() == 0);
            topoResource.getChildList().add(realTimeResource);
        }
        list.add(topoResource);
        // 拓扑目录树

        // //视图及资源
        // list.addAll(buildResView());
        ResView[] resView = SpringContextHolder.getBean(ResViewCache.class).findAll();
        for (int i = 0; i < resView.length; i++) {
            Resource viewRes = new Resource("BSM_V_" + resView[i].getId(), resView[i].getName(), FunctionConstants.TYPE_VIEW_FUNCTION, "V_" + resView[i].getId(), null);
            if (resView[i].getId() == 1) {
                List<Res> appList = SpringContextHolder.getBean(ResCache.class).getByResType(1105050000000L);
                for (Res root : appList) {
                    Resource res = new Resource("BSM_R_" + root.getId(), root.getName(), FunctionConstants.TYPE_RES_FUNCTION, "R_" + root.getId(), "BSM_V_" + resView[i].getId());
                    List clist = RelationHelper.children(root.getId());
                    res.setLeaf(clist.size() > 0 ? false : true);
                    viewRes.getChildList().add(res);
                }

            } else if (resView[i].getId() == 2) {
                List<MonitorType> mtList = SpringContextHolder.getBean(ResViewService.class).findAllMonitorType();
                for (MonitorType type : mtList) {
                    Resource mRes = new Resource("BSM_M_" + type.getId(), type.getName(), "viewMoniType", "V_" + resView[i].getId(), null);

                    mRes.setLeaf(false);
                    viewRes.getChildList().add(mRes);
                }
            } else {
                List<ResViewRel> resViewRels = SpringContextHolder.getBean(ResViewRelCache.class).getResViewRelByViewId(resView[i].getId());
                if (resViewRels != null) {
                    for (ResViewRel resViewRel : resViewRels) {
                        ResBean resBean = ResBean.get(resViewRel.getResId());
                        if (resBean != null) {
                            Resource res = new Resource("BSM_R_" + resBean.res().getId(), resBean.res().getName(), FunctionConstants.TYPE_RES_FUNCTION, "R_" + resBean.res().getId(), "BSM_V_" + resViewRel.getResViewId());
                            List clist = RelationHelper.children(resBean.res().getId());
                            res.setLeaf(clist.size() > 0 ? false : true);
                            viewRes.getChildList().add(res);
                        }
                    }
                }
            }
            list.add(viewRes);
        }
        return list;
    }
}
